<?php

namespace App\Http\Controllers;

use App\Models\MemberCoupon;
use App\Models\RedRecord;
use Illuminate\Http\Request;
use Kuyuan\Phpwidget\Str;

class RedRecordController extends Controller
{
    /**
     * 获取一个红包的领取记录列表
     */
    public function index($_id,Request $request)
    {
        $_limit = $request->input('limit',[0,10]);
        $RedRecord = RedRecord::where("red_id",$_id)
            ->where("pid",0)
            ->select("id","member_id","money","created_at")
            ->offset($_limit[0])
            ->limit($_limit[1])
            ->orderByRaw("member_id=".auth("member")->id()." DESC")
            ->get();
        if($RedRecord) {
            foreach ($RedRecord as $item) {
                $item->setAppends(['time']);
                $item->member;
                $item->money = Str::format_money($item->money);
                $RedRecordAward = RedRecord::where("pid",$item->id)
                    ->select("member_id","money")->first();
                if($RedRecordAward) {
                    $RedRecordAward->member;
                    $item->share_award = $RedRecordAward;
                    $RedRecordAward->money = Str::format_money($RedRecordAward->money);
                }
            }
        }
        return response(["data"=>$RedRecord]);
    }

    /**
     * 开启红包的金额和奖品
     * @param int $_red_id 红包id
     * @return mixed
     */
    public function show($_red_id)
    {
        $_data = array();
        $_member_id = auth('member')->id();
        $RedRecord = RedRecord::where("member_id",$_member_id)
            ->where("red_id",$_red_id)
            ->where("pid",0)
            ->first();
        if($RedRecord) {
            // 金额
            $_data["money"] = Str::format_money($RedRecord->money);
            $_data["to_go"] = $RedRecord->go_to;
            // 助力红包奖励
            $ShareRecord = RedRecord::where("pid",$RedRecord->id)
                ->first();
            if ($ShareRecord) {
                $ShareRecord->setAppends([
                    'nickname'
                ]);
                $_data["share"] = array("nickname" => $ShareRecord->nickname, "money" => Str::format_money($ShareRecord->money));
            }
            // 卡券
            $MemberCoupon = MemberCoupon::where("red_id",$_red_id)
                ->where("member_id",$_member_id)
                ->select('id','coupon_id','indate','status')
                ->first();
            if ($MemberCoupon) {
                $MemberCoupon->setAppends([
                    'coupon'
                ]);
                $_data["coupon"] = $MemberCoupon->coupon;
                $_data["coupon"]["id"] = $MemberCoupon->id;
            }
            $Response = response(['data'=>$_data]);
        } else {
            $Response = response(['code'=>402]);
        }
        return $Response;
    }

    /**
     * 当前用户的收入记录
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function income(Request $request)
    {
        $_limit = $request->input('limit',[0,10]);
        $RedRecord = RedRecord::where("member_id",auth('member')->id())
                ->select('id','money','pid','created_at')
                ->offset($_limit[0])
                ->limit($_limit[1])
                ->orderBy("created_at","desc")
                ->get();
        if($RedRecord) {
            foreach($RedRecord as $item) {
                $item->setAppends(['time']);
                $item->money = Str::format_money($item->money);
            }
        }
        return response(["data"=>$RedRecord]);
    }
}
